package pers.ht.mbpo.dao;

import pers.ht.mbpo.bean.User;
import pers.ht.mbpo.util.DBUtil;
import pers.ht.mbpo.util.DaoUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    //添加
    public void insert(User u) {
        String sql = "INSERT INTO user(username,password,nickname,college,major) VALUES (?,?,?,?,?)";
        DaoUtil.update(sql, u.getUsername(), u.getPassword(), u.getNickname(),u.getCollege(),u.getMajor());
    }

    //删除
    public void delete(String id) {
        String sql = "DELETE FROM user WHERE username = ?";
        DaoUtil.update(sql, id);
    }

    //修改
    public void edit(User u) {
        String sql = "UPDATE user SET password = ?,nickname = ? ,nickname= ? ,college= ? ,major= ?  WHERE username = ?";
        DaoUtil.update(sql, u.getPassword(), u.getNickname(),u.getCollege(),u.getMajor(), u.getUsername());
    }

    //校验主键唯一
    public boolean checkId(String id) {
        return DaoUtil.checkId("user", "username", id);
    }

    //查询所有
    public List<User> query() {
        List<User> users = new ArrayList<User>();
        String sql = "SELECT * from user WHERE 1=1";
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                User u = new User();
                u.setUsername(rs.getString("username"));
                u.setPassword(rs.getString("password"));
                u.setNickname(rs.getString("nickname"));
                u.setCollege(rs.getString("college"));
                u.setMajor(rs.getString("major"));
                users.add(u);
            }
            DBUtil.close(rs, stat, conn);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return users;
    }

    //按主键查询一个
    public User queryOne(String username) {
        User u = new User();
        String sql = "SELECT * from user WHERE username = '" + username + "'";
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                u.setUsername(rs.getString("username"));
                u.setPassword(rs.getString("password"));
                u.setNickname(rs.getString("nickname"));
                u.setCollege(rs.getString("college"));
                u.setMajor(rs.getString("major"));
            }
            DBUtil.close(rs, stat, conn);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return u;
    }
    //重置密码
    public void reSetPwd(String id) {
        String sql = "UPDATE user SET password = ?  WHERE username = ?";
        DaoUtil.update(sql, id, id);
    }
}
